package main;

import javax.microedition.lcdui.Graphics;

import control.TextControl;

public class Tutorial {
	MainMidlet midlet;
	int stepIndex = 0;
	Step[] step;
	TextControl text;

	public Tutorial(MainMidlet midlet) {
		this.midlet = midlet;
		step = new Step[20];
		step[0] = new Step(
				"You have no hero to protect the\nland. Lets hire one.", 0,
				360, 50, 40);
		step[1] = new Step("Put him here.", 30, 150, 30, 30);
		step[2] = new Step("Now, buy a fences.", 50, 360, 50, 40);
		step[3] = new Step("Put it here.", 180, 120, 30, 30);
		step[4] = new Step("Confirm.", 160, 360, 40, 40);
		step[5] = new Step("Right, let use some items.", 100, 360, 50, 40);
		step[6] = new Step("No item in your inventory. Check out\nthe store.",
				50, 360, 50, 40);
		step[7] = new Step("Ok, see this bow.", 0, 280, 40, 40);
		step[8] = new Step("Buy it.", 200, 200, 40, 40);
		step[9] = new Step("Done with the store.", 200, 360, 40, 40);
		step[10] = new Step("Now you can equip the bow to the\nhero.", 30, 150,
				30, 30);
		step[11] = new Step("Press here.", 0, 360, 50, 40);
		step[12] = new Step("Let change his arm.", 10, 310, 40, 40);
		step[13] = new Step("Sellect the bow.", 0, 240, 40, 40);
		step[14] = new Step("Confirm.", 200, 200, 40, 40);
		step[15] = new Step(
				"It seem this hero is not in a good\nposition. Move him.", 50,
				360, 50, 40);
		step[16] = new Step("Move him here.", 120, 90, 30, 30);
		step[17] = new Step("Bring it on.", 190, 360, 50, 40);
		step[18] = new Step("", 0, 30, 240, 300);
		step[19] = new Step(
				"You finished the basic lession. Now,\nit up to you.", 210, 0,
				30, 30);
		text = new TextControl(step[0].text, 10, 40);
	}

	public boolean pointerPressed(int x, int y) {
		if (step[stepIndex].hover(x, y)) {
			return true;
		}
		return false;
	}

	public boolean pointerReleased(int x, int y) {
		if (step[stepIndex].hover(x, y) && step[stepIndex].text.length() > 0) {
			stepIndex++;
			System.out.println(stepIndex);
			if (stepIndex == step.length) {
				stepIndex=0;
			}
				/*PlayingCanvas.getInstance().stop();
				midlet.showSellectGameCanvas();
				stepIndex = 0;
				System.out.println("done");
				return false;
*/
			//} else {
				text.setText(step[stepIndex].text);
				return true;
			//}
		}
		return false;
	}

	public boolean pointerDragged(int x, int y) {
		return false;
	}

	public void endRush() {

		stepIndex++;
		text.setText(step[stepIndex].text);
	}

	public void paint(Graphics g) {

		if (stepIndex >= step.length) {
			return;
		}
		if (step[stepIndex].text.length() > 0) {
			g.setColor(0x00ffffff);
			if (step[stepIndex].y > 90) {
				g.drawLine(120, 75, step[stepIndex].x + step[stepIndex].w / 2,
						step[stepIndex].y);

			} else {
				g.drawLine(120, 40, step[stepIndex].x, step[stepIndex].y
						+ step[stepIndex].h / 2);
			}
			g.fillRoundRect(5, 35, 230, 45, 10, 10);

			g.drawRect(step[stepIndex].x, step[stepIndex].y, step[stepIndex].w,
					step[stepIndex].h);
			text.paint(g);
		}
	}

}

class Step {
	String text;

	int x;
	int y;
	int w;
	int h;

	public Step(String text, int x, int y, int w, int h) {
		this.text = text;
		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
	}

	public boolean hover(int px, int py) {
		return (px > x && px < x + w && py > y && py < y + h);
	}
}